﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SaleSoft.Models;
using SaleSoft.Helper;
using System.Data;

namespace SaleSoft.Areas.administrator.Controllers
{
    public class NoiDungWebController : BaseController
    {
        //
        // GET: /administrator/NoiDungWeb/

        SaleSoftEntities db = new SaleSoftEntities();

        public ActionResult Index()
        {
            ViewData["error"] = this.TempData["error"];
            List<ds_noidungweb> _ds_noidungweb = db.ds_noidungweb.ToList();
            return View(_ds_noidungweb);
        }

        public ActionResult ThemNoiDungWeb()
        {
            ViewBag.id_loainoidung = SelectListHelper.DSloainoidung(null);
            ViewBag.id_trangthai = SelectListHelper.DSTrangThai(5, null);
            ViewBag.id_album = SelectListHelper.DSAlbum(null);
            return View();
        }

        public ActionResult SuaNoiDungWeb(int id)
        {
            ds_noidungweb _ds_noidungweb = db.ds_noidungweb.Single(d => d.id_noidungweb == id);
            ViewBag.id_loainoidung = SelectListHelper.DSloainoidung(_ds_noidungweb.id_loainoidung);
            ViewBag.id_trangthai = SelectListHelper.DSTrangThai(5, _ds_noidungweb.id_trangthai);
            ViewBag.id_album = SelectListHelper.DSAlbum(_ds_noidungweb.id_album);
            return View(_ds_noidungweb);

        }
        [HttpPost]
        public ActionResult ThemNoiDungWeb(ds_noidungweb _ds_noidungweb, HttpPostedFileBase img)
        {
            if (ModelState.IsValid)
            {
                if (img != null && img.ContentLength > 0) {
                    if (img.FileName.Substring(img.FileName.LastIndexOf(".")).ToLower() != ".jpg" && img.FileName.Substring(img.FileName.LastIndexOf(".")).ToLower() != ".png") {
                        ViewData["error"] = "Accept JPGE and PNG file only";
                        ViewBag.id_loainoidung = SelectListHelper.DSloainoidung(_ds_noidungweb.id_loainoidung);
                        ViewBag.id_trangthai = SelectListHelper.DSTrangThai(5, _ds_noidungweb.id_trangthai);
                        ViewBag.id_album = SelectListHelper.DSAlbum(_ds_noidungweb.id_album);
                        return View(_ds_noidungweb);
                    } else {
                        string dt = DateTime.Now.Year.ToString() + DateTime.Now.Month + DateTime.Now.Day + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second;
                        string fName = _ds_noidungweb.id_noidungweb + dt + img.FileName.Substring(img.FileName.LastIndexOf("."));
                        string SaveLocation = Server.MapPath("~\\UploadImages\\");
                        _ds_noidungweb.urlhinhanh = fName;
                        img.SaveAs(SaveLocation + fName);
                    }
                }
                db.ds_noidungweb.AddObject(_ds_noidungweb);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.id_loainoidung = SelectListHelper.DSloainoidung(_ds_noidungweb.id_loainoidung);
            ViewBag.id_trangthai = SelectListHelper.DSTrangThai(5, _ds_noidungweb.id_trangthai);
            ViewBag.id_album = SelectListHelper.DSAlbum(_ds_noidungweb.id_album);
            return View(_ds_noidungweb);  
        }

        [HttpPost]
        public ActionResult SuaNoiDungWeb(ds_noidungweb _ds_noidungweb, HttpPostedFileBase img)
        {
            if (ModelState.IsValid)
            {
                ds_noidungweb old = db.ds_noidungweb.Single(p => p.id_noidungweb == _ds_noidungweb.id_noidungweb);
                db.ds_noidungweb.Detach(old);
                if (img == null || img.ContentLength == 0) { // khong lam gi ca
                    _ds_noidungweb.urlhinhanh = old.urlhinhanh;
                } else if (old.urlhinhanh == null && img != null && img.ContentLength > 0) { // hinh cu khong co, luu hinh moi
                    //save image
                    if (img.FileName.Substring(img.FileName.LastIndexOf(".")).ToLower() != ".jpg" && img.FileName.Substring(img.FileName.LastIndexOf(".")).ToLower() != ".png") {
                        ViewBag.id_loainoidung = SelectListHelper.DSloainoidung(_ds_noidungweb.id_loainoidung);
                        ViewBag.id_trangthai = SelectListHelper.DSTrangThai(5, _ds_noidungweb.id_trangthai);
                        ViewBag.id_album = SelectListHelper.DSAlbum(_ds_noidungweb.id_album);
                        ViewData["error"] = "Accept JPGE and PNG file only";
                        return View(_ds_noidungweb);
                    }
                    string dt = DateTime.Now.Year.ToString() + DateTime.Now.Month + DateTime.Now.Day + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second;
                    string fName = _ds_noidungweb.id_noidungweb + dt + img.FileName.Substring(img.FileName.LastIndexOf("."));
                    string SaveLocation = Server.MapPath("~\\UploadImages\\");
                    try {
                        img.SaveAs(SaveLocation + fName);   //save new file
                        _ds_noidungweb.urlhinhanh = fName;              //save file name to entity                    
                    } catch (Exception e) { ViewData["error"] = e.Message; return View(_ds_noidungweb); }
                } else if (old.urlhinhanh != null && img != null && img.ContentLength > 0) { // co hinh cu, co hinh moi 
                    string oldFile = Server.MapPath("/UploadImages/") + old.urlhinhanh;
                    //xoa file cu
                    if (System.IO.File.Exists(oldFile)) {
                        try {
                            System.IO.File.Delete(oldFile);
                        } catch (Exception) {
                        }
                    }
                    //luu file moi
                    if (img.FileName.Substring(img.FileName.LastIndexOf(".")).ToLower() != ".jpg" && img.FileName.Substring(img.FileName.LastIndexOf(".")).ToLower() != ".png") {
                        ViewData["error"] = "Accept JPGE and PNG file only";
                        return View(_ds_noidungweb);
                    }
                    string dt = DateTime.Now.Year.ToString() + DateTime.Now.Month + DateTime.Now.Day + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second;
                    string fName = _ds_noidungweb.id_noidungweb + dt + img.FileName.Substring(img.FileName.LastIndexOf("."));
                    string SaveLocation = Server.MapPath("~\\UploadImages\\");
                    try {
                        img.SaveAs(SaveLocation + fName);   //save new file
                        _ds_noidungweb.urlhinhanh = fName;              //save file name to entity                    
                    } catch (Exception e) { 
                        ViewData["error"] = e.Message;
                        ViewBag.id_loainoidung = SelectListHelper.DSloainoidung(_ds_noidungweb.id_loainoidung);
                        ViewBag.id_trangthai = SelectListHelper.DSTrangThai(5, _ds_noidungweb.id_trangthai);
                        ViewBag.id_album = SelectListHelper.DSAlbum(_ds_noidungweb.id_album);
                        return View(_ds_noidungweb); 
                        
                    }
                }

                db.ds_noidungweb.Attach(_ds_noidungweb);
                db.ObjectStateManager.ChangeObjectState(_ds_noidungweb, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.id_loainoidung = SelectListHelper.DSloainoidung(_ds_noidungweb.id_loainoidung);
            ViewBag.id_trangthai = SelectListHelper.DSTrangThai(5, _ds_noidungweb.id_trangthai);
            ViewBag.id_album = SelectListHelper.DSAlbum(_ds_noidungweb.id_album);
            return View(_ds_noidungweb);
        }
        public ActionResult XoaNoiDungWeb(int id)
        {
            ds_noidungweb _ds_noidungweb = db.ds_noidungweb.Single(d => d.id_noidungweb == id);
            db.ds_noidungweb.DeleteObject(_ds_noidungweb);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
    }
       
}
